const app = getApp()
import {urls} from '../../../utils/urls'
Page({
  data:{
    username:'',
    password:'',
  },
  ToCommonLogin(){
    wx.navigateBack(
      {
        delta:1
      }
    )
  },
  ddinput(e){
    let name = e.currentTarget.dataset.name;
    // console.log(name);
    this.data[name] = e.detail.value;
  },
  authorLogin(){
    if(this.data.username == ''){
      wx.showToast({
        title: '请填写用户名',
        icon:'none'
      })
    }else if(this.data.password == ''){
      wx.showToast({
        title: '请填写密码',
        icon:'none'
      })
    }else{
      wx.showLoading({
        title: '登录中',
        mask:true
      })
      // let userInfo = wx.getStorage(user)
      let userInfo = {"school":wx.getStorageSync('school').id}
      userInfo.username =this.data.username
      userInfo.password = this.data.password
      // console.log(userInfo)
      // console.log(urls);
      // 然后更新后端数据库中的数据
      app.http('POST',urls.login,userInfo, function (res) {
        wx.hideLoading()
        if(res.code == 100){
          wx.showToast({
            title: '登录成功',
            mask:true,
            duration:800
          })
          let user = res.results
          let token = res.token
          wx.setStorageSync("user",user)
          wx.setStorageSync("token",token)
          setTimeout(function(){
            wx.switchTab({
              url: '/pages/mine/mine',
            })
          },900)
        }else{
          wx.showToast({
            title: '登录失败',
            icon:'none'
          })
        }
      })
    }
  },
})